package com.xiaotu.spider.pipeline;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.xiaotu.common.model.TVDanMuCommentModel;
import com.xiaotu.common.util.DateUtils;
import com.xiaotu.common.util.FileUtil;
import com.xiaotu.common.util.PropertiesUtil;
import com.xiaotu.common.util.RegexUtils;
import com.xiaotu.common.util.SepratorUtil;
import com.xiaotu.common.util.StringUtil;
import com.xiaotu.spider.APipeline;
import com.xiaotu.storage.PlatformDanMuStorageProcessor;

import net.sf.json.JSONObject;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;

@Component("ResDanMuPipeline")
@Scope("prototype")
public class ResDanMuPipeline extends APipeline{
	
	@Override
	protected void doProcess(ResultItems resultItems, Task task) {
        List<TVDanMuCommentModel> tvList = resultItems.get(TVDanMuCommentModel.class.getName());
        if (tvList != null)
            tvList.forEach(tvModel -> this.writeFile(tvModel));
	}
	private void writeFile(TVDanMuCommentModel tv)
    {
        JSONObject tvObj = JSONObject.fromObject(tv);
        List<Object> list = new ArrayList<>();
        Arrays.stream(PlatformDanMuStorageProcessor.FIELD_ARR).limit(PlatformDanMuStorageProcessor.FIELD_ARR
                .length - 1).forEach(field -> list.add(tvObj.get(field)));
        String tvName = RegexUtils.regexChStr(tv.getTvName()).trim();
        String fileName = PropertiesUtil.getProperty("file.danmu.path") + tvName+"_"+DateUtils.transferLongToDateString("yyyyMMdd", System.currentTimeMillis()) + ".csv";
       
        FileUtil.writeFile(fileName, StringUtil.list2CSVLine(list, SepratorUtil.SEP_PIPE) +
                SepratorUtil.LINE_SEPRATOR);
    }
}
